﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DataLib.Common;
using DataLib.Accounts.Bus;
using DataLib.Model;

namespace Ewts.Project
{
    public partial class ProjectAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] == null)
                {
                    Response.Redirect("/Accounts/Login.aspx");
                }
                else
                {
                    BindAddress();

                    if (!string.IsNullOrEmpty(Request.QueryString["ProjectID"]))
                    {
                        BindUpdateInfo();
                    }
                    else
                    {
                        DataBindDetile();
                    }
                }
            }
        }
        /// <summary>
        /// 绑定归属地
        /// </summary>
        private void BindAddress()
        {
            string strWhere = " flag1='01' ";
            DataLib.BLL.City bllLib = new DataLib.BLL.City();
            this.serverPoint.DataSource = bllLib.GetList(strWhere);
            this.serverPoint.DataValueField = "cityname";
            this.serverPoint.DataTextField = "cityname";
            this.serverPoint.DataBind();
        }
        private void DataBindDetile()
        {
            int id = 0;
            
            if (Request["cumid"] != null && Request["cumid"].ToString().Trim() != "")
            {
                id = int.Parse(Request["cumid"].ToString());
                hidcumid.Value = id.ToString();
                DataLib.Model.Cumtomer cum = new DataLib.BLL.Cumtomer().GetModel(id);
                if (cum != null)
                {
                    txtCjoName.Value = cum.companyname;
                    txtPrintName.Value = cum.companyname;
                    txtLabName.Value = cum.companyname;
                }
            }
            string strwhere = "IsDel = 0 and CustomerID = '" + id + "'";
            DataLib.BLL.Project pBll = new DataLib.BLL.Project();
            DataTable dt = pBll.GetList(strwhere).Tables[0];
            this.rept.DataSource = dt;
            this.rept.DataBind();
        
        }
        /// <summary>
        /// 绑定修改信息
        /// </summary>
        private void BindUpdateInfo()
        {
            int projectid = Convert.ToInt32(Request.QueryString["ProjectID"].ToString());
            DataLib.BLL.Project pBll = new DataLib.BLL.Project();
            DataLib.Model.Project pmodel = new DataLib.Model.Project();
            pmodel = pBll.GetModel(projectid);
            txtCjoName.Value = pmodel.cjoname;
            txtPrintName.Value = pmodel.printname;
            txtLabName.Value = pmodel.labname;
            hidcumid.Value = pmodel.CustomerID.ToString();

            this.txtProjectName.Text = pmodel.ProjectName;
            this.serverPoint.SelectedValue = pmodel.belongAddress;
            this.Button1.Text = "保存项目";
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = 0;

            //2015年4月18日 20:55:26 FIFA 
            string strErr = "";
            DataLib.Model.Project pmodel = new DataLib.Model.Project();
            if (Request["cumid"] != null && Request["cumid"].ToString().Trim() != "")
            {
                id = int.Parse(Request["cumid"].ToString());
              
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ProjectID"]))
            {
                int projectid = Convert.ToInt32(Request.QueryString["ProjectID"].ToString());
                DataLib.BLL.Project pBll = new DataLib.BLL.Project();
               
                if (string.IsNullOrEmpty(txtProjectName.Text.Trim()))
                {
                    strErr += "项目名称为不能为空!\\n";
                }

                pmodel = pBll.GetModel(projectid);
                pmodel.ProjectName = this.txtProjectName.Text.Trim();
                pmodel.belongAddress = this.serverPoint.SelectedValue;
                pmodel.cjoname = txtCjoName.Value;
                pmodel.printname = txtPrintName.Value;
                pmodel.labname = txtLabName.Value;
                pBll.Update(pmodel);

                pBll.updatelist(this.txtProjectName.Text.Trim(), "ServerArea", " where ProjectID =" + projectid.ToString(),"ProjectName");

                pBll.updatelist(this.txtProjectName.Text.Trim(), "ProjectsRelation", " where ProjectID =" + projectid.ToString(), "ProjectName");

                Response.Redirect("ProjectAdd.aspx?cumid=" + id.ToString());
            }
            else
            {
                DataLib.BLL.Project pBll = new DataLib.BLL.Project();
                int customerid = id;
                DataLib.Model.Cumtomer cModel = new DataLib.Model.Cumtomer();
                DataLib.BLL.Cumtomer cBll = new DataLib.BLL.Cumtomer();
                cModel = cBll.GetModel(customerid);

                //2015年4月18日 17:59:49 FIFA 增加了cModel的验证
                string cumtomercode = "";
                if (cModel != null && cModel.id > 0)
                {
                     cumtomercode = cModel.customercode;
                }
                    

                DataLib.Model.Project pModel1 = pBll.GetCustomerTop(customerid);
                if (pModel1 != null )
                {
                    string projectNum = "";
                    DataSet dt = pBll.GetList(" ProjectNum like '%" + cumtomercode + "%' order by ProjectNum");
                    for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                    {
                        projectNum = dt.Tables[0].Rows[i]["ProjectNum"].ToString();
                        int j = int.Parse(projectNum.Substring(projectNum.Length - 2, 2).ToString());
                        if (i+1 != j)
                        {
                            pmodel.ProjectNum = projectNum.Substring(0, pModel1.ProjectNum.Length - 2) + (int.Parse(projectNum.Substring(projectNum.Length - 2, 2)) - 1).ToString().PadLeft(2, '0');
                            break;
                        }
                        pmodel.ProjectNum = pModel1.ProjectNum.Substring(0, pModel1.ProjectNum.Length - 2) + (int.Parse(pModel1.ProjectNum.Substring(pModel1.ProjectNum.Length - 2, 2)) + 1).ToString().PadLeft(2, '0');
                    }             
                }
                else
                {
                    pmodel.ProjectNum = cumtomercode + "01";

                }

                pmodel.CustomerID = cModel.id;
                pmodel.CustomerNum = cModel.customercode;

                //2015年4月18日 20:50:13 FIFA 检查项目名称是否为空
                if (string.IsNullOrEmpty(txtProjectName.Text.Trim()))
                {
                    strErr += "项目名称为不能为空! \\n";               
                }
                pmodel.ProjectName = this.txtProjectName.Text.ToString();
                if (Session["UserInfo"] != null)
                {
                    User curUser = new User();
                    curUser = (User)Session["UserInfo"];
                    pmodel.belongAddress = this.serverPoint.SelectedValue;
                    pmodel.CreateDatetime = DateTime.Now;
                    pmodel.CreateOper = curUser.UserName;
                }
                pmodel.cjoname = txtCjoName.Value;
                pmodel.printname = txtPrintName.Value;
                pmodel.labname = txtLabName.Value;

                //2015年4月18日 20:58:07 FIFA
                if (!string.IsNullOrEmpty(strErr))
                {
                    MessageBox.Show(this,strErr);
                    return;
                }
                pBll.Add(pmodel);
                DataBindDetile();
            }   
        }
        /// <summary>
        /// 删除项目事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDel_Click(object sender, CommandEventArgs e)
        {
            int ProjectID = Convert.ToInt32(e.CommandName);
            new DataLib.BLL.ProjectsRelation().RelationDelete(0, 0, 0, ProjectID, 4);
            DataBindDetile();
        }

        protected string GetCumName(string cumid)
        {
            DataLib.BLL.Cumtomer bll = new DataLib.BLL.Cumtomer();
            DataLib.Model.Cumtomer model = new DataLib.Model.Cumtomer();
            model = bll.GetModel(int.Parse(cumid));
            return model.companyname;
        }
    }

}